﻿using MongoDB.Bson;
using MongoDB.Driver;
/*----------------------------------------------------------------
// Copyright (C) ET
// 版权所有。 
//
// 文件名：DBdb2

// 文件功能描述：RTDBHelper操作

// 创建标识：胡进财 20140822
 
// 修改标识：

// 修改描述：

//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.OperationDB
{
    public class RTDBHelper
    {

        /// <summary>
        /// 实时数据库操作父类，在此类定义数据库操作要用到的方法
        /// </summary>
        public virtual int OpenSR(ref int handle)
        {
            return 0;
        }
        public virtual object closeSR(int handle)
        {
            object obj = null;
            return obj;
        }
        public virtual object GetRealTimeValue(string tagName, int bandle)//返回实时值
        {
            return 0;
        }
        public virtual double[] GetRealTimeValues(int count, string[] tagName, int bandle)//返回实时值数组
        {
            double[] value = new double[count];
            return value;
        }
        public virtual double GetHisValue(string tagName, string hisTime, int bandle)//返回历史值
        {
            return 0;
        }
        /// <summary>
        /// 获取单个标签点一段时间内等间隔历史插值
        /// 创建：李东峰 日期：2014-05-06
        /// 修改：
        /// 修改内容：
        ///
        /// </summary>
        /// <param name="count">想要获取的历史值的个数</param>
        /// <param name="tagName">标签点名称</param>
        /// <param name="startTime">起始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="handle">数据库连接句柄</param>
        /// <returns>返回取到的历史值数组</returns>
        public virtual double[] GetHisValueByStartTimeAndEndTime(int count, string tagName, string startTime, string endTime, int handle)
        {
            double[] value = new double[count];
            return value;
        }
        public virtual int SetRealTimeValue(ref string tagName, ref object val, int bandle)//写实时值
        {
            return 0;
        }
        public virtual int SetHisValue(ref string tagName, ref string hisTime, ref object val, int bandle)//写历史值
        {
            return 0;
        }
        public virtual void GetHisValue(string pName, string time, ref double val) { }

        public virtual double[] GetHisValue(string tag, DateTime sTime, DateTime eTime)
        {
            double[] value = null;
            return value;
        }
        public virtual double[] GetHisValue(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] value = null;
            return value;
        }

        public virtual DataTable GetHisValDt(string tag, DateTime sTime, DateTime eTime)
        {
            DataTable dt = new DataTable();
            return dt;
        }
        public virtual void GetHisValue(string pName, string date, ref int ret, ref double val) { }
        public virtual void GetHisValueByTime(string pName, string date, ref int ret, ref double val) { }
        public virtual void GetHisDiffValue(string pName, string bTime, string eTime, ref int ret, ref double val) { }
        public virtual void GetHisDiffValue(string pName, string bTime, ref int ret, ref double val) { }
        public virtual void GetHisDiffValueByTime(string pName, string bTime, string eTime, ref int ret, ref double val) { }
        public virtual string GetPoint()
        {
            return "";
        }
        public virtual int DateToUTC()
        {
            return 0;
        }
        public virtual string RetPointValueAvg(string tag, int type, string date)
        {
            return "";
        }
        public virtual string RetPointDiffValue(string tag, string bt, string et)
        {
            return "";
        }

    }

    public class SQL : RTDBHelper
    {
        DBsql sql = null;
        public SQL(string strIniPath, string strIniRealType, string strIniName)
        {
            sql = new DBsql(strIniPath, strIniName, strIniRealType);
        }

        public override object GetRealTimeValue(string tagName, int handle)
        {
            return sql.GetRealTimeVal(tagName);
        }

        public override double GetHisValue(string tagName, string hisTime, int handle)
        {
            return sql.GetHisVal(tagName);
        }

        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime)
        {
            return sql.GetHisVal(tag, sTime, eTime);
        }

        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime, int second)
        {
            return sql.GetHisVal(tag, sTime, eTime, second);
        }

        public override DataTable GetHisValDt(string tag, DateTime sTime, DateTime eTime)
        {
            return sql.GetHisValDt(tag, sTime, eTime);
        }



    }

    public class Mongo : RTDBHelper
    {
        MongoDB mg = null;
        public Mongo(string strIniPath, string strIniRealType, string strIniName)
        {
            mg = new MongoDB(strIniPath, strIniName, strIniRealType);
        }

        public override object GetRealTimeValue(string tagName, int handle)
        {
            return mg.GetRealValue(tagName);
        }

        public override double GetHisValue(string tagName, string hisTime, int handle)
        {
            return mg.GetHisValue(tagName, Convert.ToDateTime(hisTime));
        }

        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime)
        {
            return mg.GetHisVal(tag, sTime, eTime);
        }
        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime, int second)
        {
            return mg.GetHisVal(tag, sTime, eTime, second);
        }

        public override DataTable GetHisValDt(string tag, DateTime sTime, DateTime eTime)
        {
            return mg.GetHisValDt(tag, sTime, eTime);
        }

        public override int SetHisValue(ref string tagName, ref string hisTime, ref object val, int handle)
        {
            List<BsonDocument> list = new List<BsonDocument>();
            BsonDocument bd = new BsonDocument();
            bd.Add("Tag", tagName);
            list.Add(bd);
            mg.AddVal(list);
            return 1;
        }
    }

    public class Pi : RTDBHelper
    {
        Plink pl = null;
        public Pi(string strIniPath, string strIniRealType, string strIniName)
        {
            pl = new Plink(strIniPath, strIniRealType, strIniName);
        }

        public override int OpenSR(ref int handle)
        {
            return pl.OpenSR();
        }
        public override object closeSR(int handle)
        {
            return pl.closePi();
        }
        public override object GetRealTimeValue(string tagName, int handle)
        {
            return pl.returnValueByTagName(tagName);
        }
        public override double[] GetRealTimeValues(int count, string[] tagName, int handle)
        {
            double[] value = new double[count];
            return value;
        }
        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime)
        {
            double[] value = null;
            return value;
        }
        public override DataTable GetHisValDt(string tag, DateTime sTime, DateTime eTime)
        {
            DataTable dt = new DataTable();
            return dt;
        }
        public override double[] GetHisValue(string tag, DateTime sTime, DateTime eTime, int second)
        {
            double[] value = null;
            return value;
        }
        public override double GetHisValue(string tagName, string hisTime, int handle)
        {
            return pl.returnValueByTagName(tagName, hisTime);
        }
        public override int SetRealTimeValue(ref string tagName, ref object val, int handle)
        {
            return pl.SetCurValue(ref tagName, ref val);
        }
        public override int SetHisValue(ref string tagName, ref string hisTime, ref object val, int handle)
        {
            return pl.SetHisValue(ref tagName, ref hisTime, ref val);
        }
        public override void GetHisValue(string pName, string time, ref double val)
        {
            pl.GetHisValue(pName, time, ref val);
        }
    }
}
